package zj.it.bhne.gridengineeringsurvey.myutils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.ArrayList;

import zj.it.bhne.gridengineeringsurvey.R;
import zj.it.bhne.gridengineeringsurvey.adapter.IntruderViewPagerAdapter;


public class PopupReviewImage {

    private TextView popupTvPageNum;
    private PopupWindow popupWindow;
    //private TextView txtRight;
    private int position;
    private ArrayList<String> mList;
    private View rootVew;
    private LinearLayout popupLl;
    private ViewPager popupViewPager;
    private IntruderViewPagerAdapter adapter;

    public static PopupReviewImage getInstance() {
        return PopupReviewImage.MenuUtilHolder.INSTANCE;
    }

    /**
     * 弹起 popupWindow
     * 通过路径显示
     *
     * @param context context
     * @param parent  parent
     */
    public void show(Context context, View parent, final ArrayList<String> mList, final int position) {
        this.mList = mList;
        this.position = position;
        _createView(context);
        if (popupWindow != null && !popupWindow.isShowing()) {
            popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        }
    }

    /**
     * 创建 popupWindow 内容
     *
     * @param context context
     */
    @SuppressLint("InflateParams")
    private void _createView(final Context context) {
        rootVew = LayoutInflater.from(context).inflate(R.layout.layout_popup_image, null);
        popupWindow = new PopupWindow(rootVew,
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        //设置为失去焦点 方便监听返回键的监听
        popupWindow.setFocusable(false);
        popupWindow.setClippingEnabled(false);
        // 如果想要popupWindow 遮挡住状态栏可以加上这句代码
        //popupWindow.setClippingEnabled(false);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(true);
        initLayout(context);
    }

    /**
     * 初始化 view
     */
    @SuppressLint("SetTextI18n")
    private void initLayout(Context context) {
        popupViewPager = rootVew.findViewById(R.id.popupViewPager);
        popupTvPageNum = rootVew.findViewById(R.id.popupTvPageNum);
        popupLl = rootVew.findViewById(R.id.popupLl);
        //txtRight = rootVew.findViewById(R.id.txtRight);
        ImageView imgBack = rootVew.findViewById(R.id.imgBack);
        TextView txtTitle = rootVew.findViewById(R.id.txtTitle);
        txtTitle.setText("查看图片");

        adapter = new IntruderViewPagerAdapter(context, mList, position);

        popupViewPager.setAdapter(adapter);

        int page = position + 1;
        popupTvPageNum.setText(page + "/" + mList.size());
        popupViewPager.setCurrentItem(position);

        popupViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onPageSelected(int i) {
                int page = i + 1;
                popupTvPageNum.setText(page + "/" + mList.size());
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        imgBack.setVisibility(View.VISIBLE);
        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                close();
            }
        });

    }

    /**
     * 关闭popupWindow
     */

    private void close() {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
            popupWindow = null;
        }
    }

    /**
     * 显示右上角删除按钮
     */
   /* public ArrayList<Integer> showDelete(boolean isShow) {
        final ArrayList<Integer> integers = new ArrayList<>();
        if (isShow) {
            txtRight.setVisibility(View.VISIBLE);
            txtRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mList.remove(popupViewPager.getCurrentItem());
                    adapter.notifyDataSetChanged();
                    popupViewPager.setAdapter(adapter);
                    integers.add(popupViewPager.getCurrentItem());
                }
            });
        } else {
            txtRight.setVisibility(View.GONE);
        }
        return integers;
    }*/


    /**
     * 显示标题栏
     */
    public void showActionbar() {
        popupLl.setVisibility(View.VISIBLE);
    }

    /**
     * 关闭标题栏
     */
    public void closeActionbar() {
        popupLl.setVisibility(View.GONE);
    }

    private static class MenuUtilHolder {
        @SuppressLint("StaticFieldLeak")
        static PopupReviewImage INSTANCE = new PopupReviewImage();
    }

}
